package com.demo.personalaccountingbackend.controller;

import com.demo.personalaccountingbackend.dto.ApiResponse;
import com.demo.personalaccountingbackend.dto.RecordRequest;
import com.demo.personalaccountingbackend.service.RecordService;
import com.demo.personalaccountingbackend.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 类描述信息
 *
 * @author zgx
 * @version 1.0
 * @date 2025/10/15 16:14
 */

@RestController
@RequestMapping("/records")
@RequiredArgsConstructor
public class RecordController {

    private final RecordService recordService;

    private final JwtUtil jwtUtil;

    /**
     * 获取当前用户ID
     */
    private Long getCurrentUserId(HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        return jwtUtil.getUserIdFromToken(token);
    }

    /**
     * 获取记账记录列表
     */
    @GetMapping("/list")
    public ApiResponse getRecords(
            HttpServletRequest request,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(required = false) Integer type) {

        Long userId = getCurrentUserId(request);
        return recordService.getRecords(userId, startDate, endDate, type);
    }

    /**
     * 获取记账记录详情
     */
    @GetMapping("/{id}")
    public ApiResponse getRecordDetail(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        return recordService.getRecordDetail(id, userId);
    }

    /**
     * 添加记账记录
     */
    @PostMapping("/add")
    public ApiResponse addRecord(@Valid @RequestBody RecordRequest recordRequest, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        return recordService.addRecord(recordRequest, userId);
    }

    /**
     * 更新记账记录
     */
    @PutMapping("/{id}")
    public ApiResponse updateRecord(@PathVariable Long id, @Valid @RequestBody RecordRequest recordRequest, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        return recordService.updateRecord(id, recordRequest, userId);
    }

    /**
     * 删除记账记录
     */
    @DeleteMapping("/{id}")
    public ApiResponse deleteRecord(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        return recordService.deleteRecord(id, userId);
    }

    /**
     * 获取统计信息
     */
    @GetMapping("/statistics")
    public ApiResponse getStatistics(
            HttpServletRequest request,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

        Long userId = getCurrentUserId(request);
        return recordService.getStatistics(userId, startDate, endDate);
    }

    /**
     * 获取月度统计
     */
    @GetMapping("/statistics/monthly")
    public ApiResponse getMonthlyStatistics(
            HttpServletRequest request,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {

        Long userId = getCurrentUserId(request);
        return recordService.getMonthlyStatistics(userId, year, month);
    }
}
